package blog.gui;

import java.io.IOException;

import javax.microedition.io.ConnectionNotFoundException;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.List;

import blog.ConstString;
import blog.SysRuntime;
import blog.controller.ConstAction;
import blog.controller.ConstGui;
import blog.db.BlogEntryDB;
import blog.util.StringUtils;

public class BlogMainViewGui extends List implements IBaseGui, CommandListener {
	
	private static Command backCmd = new Command( ConstString.CMD_BACK, Command.BACK, 0 );
	private static Command okCmd = new Command( ConstString.CMD_SELECT, Command.OK, 0 );
	
	public Image draftImg;
	private Image writeImg;
	public Image historyImg;
	private Image visitImg;
	
	private static BlogMainViewGui _instance;
	public static synchronized BlogMainViewGui getInstance()
	{
		if ( _instance==null )
		{
			_instance = new BlogMainViewGui();
		}
		return _instance;
	}
	
	private BlogMainViewGui()
	{
		super(ConstString.GUI_TITLE_BLOG_MAIN_VIEW, List.IMPLICIT);
		
		try {
			draftImg = Image.createImage("/draft.png");
		} catch (IOException e) {
		}
		try {
			writeImg = Image.createImage("/writeEntry.png");
		} catch (IOException e) {
		}
		try {
			historyImg = Image.createImage("/history.png");
		} catch (IOException e) {
		}
		try {
			visitImg = Image.createImage("/visitBlog.png");
		} catch (IOException e) {
		}

		this.append( StringUtils.combin(ConstString.GUI_BLOG_MAIN_VIEW_VIEW_CACHE, "0"), draftImg);
		this.append(ConstString.GUI_BLOG_MAIN_VIEW_NEW_ENTRY, writeImg);
		//this.append(ConstString.GUI_BLOG_MAIN_VIEW_SELECT_BLOG, null);
		this.append(StringUtils.combin(ConstString.GUI_BLOG_MAIN_VIEW_SENT_HISTORY, "0"), historyImg);
		this.append(ConstString.GUI_BLOG_MAIN_VIEW_VISIT_BLOG, visitImg);
		
		this.setCommandListener(this);
		this.addCommand(backCmd);
		this.addCommand(okCmd);
	}

	public int getIdentify() {
		return ConstGui.GUI_ID_BLOG_MAIN_VIEW;
	}

	public void notifyChanged(Object param) {
		
	}

	public void commandAction(Command cmd, Displayable arg1) {
		if ( cmd==List.SELECT_COMMAND || cmd==okCmd )
		{
			switch( getSelectedIndex() )
			{
				case 0:
				{
					BlogEntryListGui nextScreen = BlogEntryListGui.getInstance();
					nextScreen.setCondition(false);
					nextScreen.reloadData();
					SysRuntime.guiManager.show( nextScreen );
					break;
				}
				case 1:
				{
					BlogEntryGui nextScreen = BlogEntryGui.getInstance();
					nextScreen.setOpType(BlogEntryGui.OP_TYPE_ADD, null);
					SysRuntime.guiManager.show( nextScreen );
					break;
				}
//				case 2:
//				{
//					SysRuntime.actionManager.addAction(this, ConstAction.ACT_GET_BLOG_LIST);
//					break;
//				}
				case 2:
				{
					BlogEntryListGui nextScreen = BlogEntryListGui.getInstance();
					nextScreen.setCondition(true);
					nextScreen.reloadData();
					SysRuntime.guiManager.show( nextScreen );
					break;
				}
				case 3:
				{
					try {
						SysRuntime.midlet.platformRequest(SysRuntime.curAccount.getBlogUrl());
					} catch (ConnectionNotFoundException e) {
						Alert alert = new Alert(ConstString.GUI_TITLE_PROMPT, ConstString.PROMPT_VISIT_BLOG_FAILED, null, AlertType.WARNING);
						SysRuntime.guiManager.showAlert(alert, true);
					}
					break;
				}
			}
		}
		else if ( cmd==backCmd )
		{
			SysRuntime.guiManager.back();
		}
	}
	
	public void reloadData()
	{
		int size = Math.min(BlogEntryDB.get( SysRuntime.curAccount.getId(), false ).length, SysRuntime.curAccount.getLenEntryList());
		this.set(0, StringUtils.combin(ConstString.GUI_BLOG_MAIN_VIEW_VIEW_CACHE, String.valueOf(size)), this.draftImg);
		size = Math.min(BlogEntryDB.get( SysRuntime.curAccount.getId(), true ).length, SysRuntime.curAccount.getLenHistoryList());
		this.set(2, StringUtils.combin(ConstString.GUI_BLOG_MAIN_VIEW_SENT_HISTORY, String.valueOf(size)), this.historyImg);
	}
}
